<?php

namespace App;

use App\Model\Article;
use App\Model\Attachment;
use App\Model\Lesson;
use App\Model\Topic;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable{
        notify as protected systemNotify;
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = [
//        'name', 'email', 'password',
//    ];
    protected $guarded = ['password_confirmation'];//不允许被批量填充的数据

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function attachment(){
        return $this->hasMany (Attachment::class);
    }
    protected function getIconAttribute($key){
        return $key?:asset('images/default.png');
    }

    public function fans(){
        return $this->belongsToMany(User::class,'followers','user_id','follower_id');
    }
    public function follower(){
        return $this->belongsToMany(User::class,'followers','follower_id','user_id');
    }
    public function isfollowing(User $user){
        return $this->follower->contains($user);
    }
    public function article(){
        return $this->hasMany(Article::class);
    }

    public function topic(){
        return $this->hasMany(Topic::class);
    }

    public function notify($instance)
    {
        if (auth()->id()!=$this->id){
            $this->systemNotify($instance);
        };
    }

    public function lesson(){
        return $this->hasMany (Lesson::class);
    }

}
